/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{
  /// <summary>
  /// Represents the root configuration for the O/R Mapping Application Block.
  /// </summary>
  [Image(typeof(ObjectMappingSettingsNode))]
  [SelectedImage(typeof(ObjectMappingSettingsNode))]
  public class ObjectMappingSettingsNode : ConfigurationNode
  {
    private SqlQueryNode sqlQueryNode;
    private ObjectSqlMappingNode sqlMappingNode;
    private EventHandler<ConfigurationNodeChangedEventArgs> sqlQueryNodeRemovedHandler;
    private EventHandler<ConfigurationNodeChangedEventArgs> sqlMappingNodeRemovedHandler;	
     
		/// <summary>
		/// Initializes a new instance of the <see cref="ObjectMappingSettingsNode"/> class.
		/// </summary>		
    public ObjectMappingSettingsNode() : base(Resource.DefaultObjectMappingSettingsNodeName)
    {
      this.sqlQueryNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnSqlQueryNodeRemoved);
      this.sqlMappingNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnSqlMappingNodeRemoved);		
	
    }

		/// <summary>
		/// <para>Releases the unmanaged resources used by the <see cref="ObjectMappingSettingsNode "/> and optionally releases the managed resources.</para>
		/// </summary>
		/// <param name="disposing">
		/// <para><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</para>
		/// </param>
		protected override void Dispose(bool disposing)
		{			
			try
			{
				if (disposing)
				{
					if (null != sqlQueryNode)
					{
						sqlQueryNode.Removed -= sqlQueryNodeRemovedHandler;
					}

          if (null != sqlMappingNode)
          {
            sqlMappingNode.Removed -= sqlMappingNodeRemovedHandler;
          }
				}
			}
			finally
			{
				base.Dispose(disposing);
			}			
		}

		/// <summary>
		/// <para>Gets the name for the node.</para>
		/// </summary>
		/// <value>
		/// <para>The display name for the node.</para>
		/// </value>		
		[ReadOnly(true)]
    public override string Name
    {
        get { return base.Name; }
    }

    /// <summary>
    /// Gets or sets the default cache manager
    /// </summary>
		/// <value>
		/// The default cache manager.
		/// </value>
    [Required]
    [SRDescription("DefaultSqlQueryDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(SqlQueryNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public SqlQueryNode DefaultSqlQuery
    {
      get { return sqlQueryNode; }
      set
      {
          sqlQueryNode = LinkNodeHelper.CreateReference<SqlQueryNode>(sqlQueryNode, 
                                                                      value, 
                                                                      sqlQueryNodeRemovedHandler, 
                                                                      null);
      }
    }
    /// <summary>
    /// Gets or sets the default cache manager
    /// </summary>
    /// <value>
    /// The default cache manager.
    /// </value>
    [Required]
    [SRDescription("DefaultSqlMappingDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(ObjectSqlMappingNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public ObjectSqlMappingNode DefaultObjectSqlMapping
    {
      get { return sqlMappingNode; }
      set
      {
        sqlMappingNode = LinkNodeHelper.CreateReference<ObjectSqlMappingNode>(sqlMappingNode,
                                                                    value,
                                                                    sqlMappingNodeRemovedHandler,
                                                                    null);
      }
    }

		/// <summary>
		/// Raises the <see cref="ConfigurationNode.ChildAdded"/> event and confirms that only one <see cref="SqlQueryCollectionNode"/> has been added.
		/// </summary>
		/// <param name="e">A <see cref="ConfigurationChangedEventArgs"/> that contains the event data.</param>
		protected override void OnChildAdded(ConfigurationNodeChangedEventArgs e)
    {
			base.OnChildAdded(e);

      //if (Nodes.Count > 1 && e.Node.GetType() == typeof(SqlQueryCollectionNode))
      //{
      //  throw new InvalidOperationException(Resource.ExceptionOnlyOneSqlQueryCollectionNode);
      //}
      //if (Nodes.Count > 1 && e.Node.GetType() == typeof(ObjectSqlMappingCollectionNode))
      //{
      //  throw new InvalidOperationException(Resource.ExceptionOnlyOneSqlMappingCollectionNode);
      //}
    }

    private void OnSqlQueryNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
        this.sqlQueryNode = null;
    }
    private void OnSqlMappingNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
        this.sqlMappingNode = null;
    }
  }
}